package com.dao;

import java.sql.*;

/**
 * @Author:wsc
 * @Date:2025/9/21-00:07-25
 */
public class BaseDao {
    // 常量
    private static final String DRIVER = "com.mysql.cj.jdbc.Driver";
    private static final String URL = "jdbc:mysql://localhost:3306/shop";
    private static final String USERNAME = "root";
    private static final String PASSWORD = "123456";

    // 加载驱动
    static {
        try {
            Class.forName(DRIVER);
        } catch (ClassNotFoundException e) {
            System.out.println("驱动加载失败");
            throw new RuntimeException(e);
        }
    }

    // 关闭资源
    public static Connection getCon() {
        Connection con = null;
        try {
            con = DriverManager.getConnection(URL,USERNAME,PASSWORD);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return con;
    }
    // 关闭资源
    public static void close(Connection con, Statement stmt, ResultSet rs) {
        if (rs != null) {
            try {
                rs.close();
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }
        if (stmt != null) {
            try{
                stmt.close();
            }catch(SQLException e){
                throw new RuntimeException(e);
            }
        }
        if (con != null) {
            try{
                con.close();
            }catch(SQLException e){
                throw new RuntimeException(e);
            }
        }
    }
}
